package com.vvkee.utils;

public class Crc16Util {
    private static final int POLY = 0x1021;

    public static int calculate(byte[] data) {
        int crc = 0xA1EC;

        for (byte b : data) {
            crc ^= (b & 0xFF) << 8;

            for (int i = 0; i < 8; i++) {
                if ((crc & 0x8000) != 0) {
                    crc = (crc << 1) ^ POLY;
                } else {
                    crc <<= 1;
                }
            }
        }

        return crc & 0xFFFF;
    }
}
